<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>添加商品</span>
      </div>
      <el-form
        :model="addForm"
        status-icon
        :rules="rules"
        ref="addForm"
        label-width="100px"
        class="demo-addForm"
      >
        <el-form-item label="商品名称" prop="name">
          <el-input
            type="text"
            v-model="addForm.name"
            autocomplete="off"
            placeholder="请输入商品名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="商品分类" prop="category">
          <el-select v-model="addForm.category" placeholder="请选择商品分类">
            <el-option
              :label="item"
              :value="item"
              v-for="item in cateList"
              :key="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品价格" prop="price">
          <el-input-number
            v-model="addForm.price"
            :precision="2"
            :step="0.1"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="商品图片" prop="imgUrl">
          <el-upload
            class="avatar-uploader"
            :action="action"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="addForm.imgUrl" :src="addForm.imgUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="活动形式" prop="goodsDesc">
          <el-input type="textarea" v-model="addForm.goodsDesc"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('addForm')"
            >提交</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { categories, goodsAdd } from "../../api/goods";
export default {
  data() {
    return {
      cateList: [], //所有分类
      action: "http://127.0.0.1:5000/goods/goods_img_upload",
      addForm: {
        name: "",
        category: "",
        price: "",
        imgUrl: "",
        goodsDesc: "",
      },
      rules: {
        name: [{ required: true, message: "请输入商品名称", trigger: "blur" }],
        category: [
          { required: true, message: "请选择商品分类", trigger: "blur" },
        ],
        price: [{ required: true, message: "请输入商品价格", trigger: "blur" }],
        imgUrl: [
          { required: true, message: "请选择商品图片", trigger: "blur" },
        ],
        goodsDesc: [
          { required: true, message: "请输入商品描述", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    submitForm(addForm) {
      this.$refs[addForm].validate(async(valid) => {
        if (valid) {
           let res = await goodsAdd(this.addForm);
           let {code,msg} = res;
           if(code===0){
             this.$message({
               type:'success',
               message:msg
             })
             this.$router.push('/goods/goodslist')
           }
        } else {
          console.log("error submit!!");
        }
      });
    },
    //上传成功
    handleAvatarSuccess(res) {
      this.addForm.imgUrl = res.imgUrl;
      console.log(res);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg" || "image/webp";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG和webp 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
  async created() {
    //获取所有启用的分类名称
    this.cateList = await categories();
    console.log(this.cateList);
  },
};
</script>

<style lang="less" scoped>
/deep/.demo-addForm {
  width: 500px;
  .el-select {
    width: 100%;
  }
}
/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>